package com.lzp.customview.text;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.Layout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.utils.SizeUtils;

/**
 * @describe
 * @author: lixiaopeng
 * @Date: 2019-06-14
 */
public class TextPathView extends View {

    private TextPaint paint;
    private Context context;
    private int paintColor = Color.RED;

    private String content = "手";

    private Path textPath;//文字的path

    //文本宽度
    private float mTextWidth;//文字的宽度
    private float mTextHeight;//文字的高度

    public TextPathView(Context context) {
        this(context, null);
    }

    public TextPathView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        initPaint();
        initTextPath();
    }

    private void drawPath(Canvas canvas) {
        canvas.save();//防止错位
        paint.setStyle(Paint.Style.STROKE);
        canvas.translate(getWidth() / 2 - mTextWidth / 2, 0);
        canvas.translate(0, getHeight() / 2 - mTextHeight / 2);
        canvas.drawPath(textPath,paint);
        canvas.restore();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawPath(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //处理包裹内容的情况
        int warpDefaultSize = SizeUtils.dip2px(context, 100);
        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            widthSize = heightSize = warpDefaultSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            widthSize = warpDefaultSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = warpDefaultSize;
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    private void initTextPath() {
        if (TextUtils.isEmpty(content)) {
            throw new RuntimeException("content can not be empty");
        }
        if (paint == null) {
            throw new RuntimeException("paint not init");
        }
        textPath = new Path();
        paint.getTextPath(content, 0, content.length(), 0, paint.getTextSize(), textPath);
    }

    private void initPaint() {
        paint = new TextPaint();
        paint.setColor(paintColor);
        paint.setStrokeWidth(5);
        paint.setTextSize(420f);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);//设置画笔末端为圆形 BUTT：无线帽   ROUND：圆形线帽    SQUARE：方形线帽
        paint.setStrokeJoin(Paint.Join.ROUND);//设置画笔转弯处我圆形 MITER：锐角 ROUND：圆弧  BEVEL：斜线
        paint.setStyle(Paint.Style.STROKE);
        //paint.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"niu.ttf"));
        mTextWidth = Layout.getDesiredWidth(content, paint);
        Paint.FontMetrics metrics = paint.getFontMetrics();
        mTextHeight = metrics.bottom - metrics.top;
    }
}
